#!/bin/sh
#
# Common utilities.
#

set -o nounset

# Used everywhere.
LFS=/home/lfs

log() {
  echo 1>&2 "$@"
}

die() {
  log "$@"
  exit 1
}

maybe_untar_cd() {
  local name=$1  # linux-3.10.10
  local ext=$2   # .tar.gz

  cd $LFS/sources

  if ! test -d "$name"; then
    log "tar xvf $name$ext"
    # TODO: --checkpoint is probably a GNU-ism?  Check BSD.
    tar -x --checkpoint=1000 --file "$name$ext"
  fi

  cd $name
}

mkdir_cd() {
  local dir=$1
  mkdir -v $dir
  cd $dir
}

# configure with /tools prefix, make, mak install.  And error checking.  A
# bunch of the GNU packages can use this simple template.
conf_build_install() {
  maybe_untar_cd "$@"

  set -o errexit
  ./configure --prefix=/tools
  make
  make install
}
